package com.sykj.bsc.wsy.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.ProcessBuilder.Redirect;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sykj.bsc.util.Constants;
import com.sykj.bsc.util.JedisUtil;
import com.sykj.bsc.wsy.model.Bis_beauty_config;
import com.sykj.bsc.wsy.service.Bis_beauty_configService;

@Controller
@RequestMapping("wsyconfig")
public class Bis_beauty_configHandler {
@Autowired
	Bis_beauty_configService service;
	/**
	 * 鏌ヨ缇庣爺甯侀厤缃〃selectBis_beauty_config
	 * @param model
	 * @param BBC 
	 * @return
	 */
	@RequestMapping("selectBis_beauty_config")
	public String selectBis_beauty_config(Model model,Bis_beauty_config BBC){
		model.addAttribute("BBCList", service.selectBis_beauty_config(BBC));
		return "backstage/wsy/Bis_beauty_configShow";
	}
	/**
	 * 鏍规嵁閰嶇疆缂栧彿ByConfig_idselectBis_beauty_config鏌ヨ缇庡甯侀厤缃〃淇℃伅
	 * @param config_id 閰嶇疆缂栧彿
	 * @param map
	 * @param res 
	 */
	@RequestMapping("findByConfig_idBis_beauty_config/{config_id}")
	@ResponseBody
	public void findByConfig_idBis_beauty_config(@PathVariable("config_id")int config_id,Map<String, Object> map,HttpServletResponse res){
		ObjectMapper mapper = new ObjectMapper();
		try {
			String jsonStr=mapper.writeValueAsString(service.findByConfig_idBis_beauty_config(config_id));
			getWirte(res, jsonStr);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
 	}
	/**
	 * 鏍规嵁閰嶇疆缂栧彿UpdateBis_beauty_config淇敼缇庡甯侀厤缃〃淇℃伅
	 * 瀵圭編鐮斿竵閰嶇疆琛╥nsertBis_beauty_config杩涜娣诲姞鎿嶄綔 
	 * @param BBC
	 * @param map
	 * @return
	 */
	@RequestMapping("updateBis_beauty_config")
	public String updateBis_beauty_config(Bis_beauty_config BBC,Map<String, Object> map){
		if (BBC.getConfig_id()==0) {
			service.insertBis_beauty_config(BBC);;
			return "redirect:/wsyconfig/selectBis_beauty_config";
		}else {
			service.updateBis_beauty_config(BBC);
			return "redirect:/wsyconfig/selectBis_beauty_config";
		}		
	}
	/**
	 * 鏍规嵁閰嶇疆缂栧彿deleteBis_beauty_config鍒犻櫎缇庣爺閰嶇疆琛ㄤ俊鎭�
	 * @param id
	 * @return
	 */
	@RequestMapping("deleteBis_beauty_config")
	public String deleteBis_beauty_config(
			Integer id){
		service.deleteBis_beauty_config(id);
		return "redirect:/wsyconfig/selectBis_beauty_config";
		
	}
	/**
	 * 杞崲鎴恓son鏁版嵁
	 * @param resp
	 * @param data
	 */
	public static void getWirte(HttpServletResponse resp,Object data) {
		try {
			if(data!=null&&!data.equals("")) {
				PrintWriter	out = resp.getWriter();
				out.print(data);
				out.flush();
				out.close();
				return;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 
	 * @param beautyNum  涓�鍏冨彲鍏戞崲澶氬皯涓鍊�
	 * @param hasDay   鏈夋晥鏈�
	 * @return
	 */
	@RequestMapping("/saveConfig")
	public String saveConfig(@RequestParam(defaultValue="0")int beautyNum,
			@RequestParam(defaultValue="0")int hasDay){
		JedisUtil.setString("beautyNum",beautyNum+"",-1);
		JedisUtil.setString("hasDay",hasDay+"",-1);
		return "redirect:/wsyconfig/selectBis_beauty_config";
	}
	
	@RequestMapping("/getDataFromRedis")
	@ResponseBody
	public String getDataFromRedis(){
		String beauty=JedisUtil.getString(Constants.BEAUTYNUM,"0");
		String hasDay=JedisUtil.getString(Constants.HAS_DAY,"0");
		String str=beauty+":"+hasDay;
		return str;
		
	}
	
	/**
	 *姹犵鍒涘缓
	 */	
}
